1
|
|
|
/* |
2
|
|
|
* Copyright (c) 2017-2018 Rafael da Silva Rocha. |
3
|
|
|
* |
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
5
|
|
|
* a copy of this software and associated documentation files (the |
6
|
|
|
* "Software"), to deal in the Software without restriction, including |
7
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
8
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
9
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
10
|
|
|
* the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be |
13
|
|
|
* included in all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
18
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
19
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
20
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
21
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @fileoverview The WaveFile class. |
27
|
|
|
* @see https://github.com/rochars/wavefile |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
/** @module wavefile */ |
31
|
|
|
|
32
|
|
|
import bitDepthLib from './vendor/bitdepth.js'; |
33
|
|
|
import * as imaadpcm from './vendor/imaadpcm.js'; |
34
|
|
|
import * as alawmulaw from './vendor/alawmulaw.js'; |
35
|
|
|
import {encode, decode} from './vendor/base64-arraybuffer-es6.js'; |
36
|
|
|
import {unpackArray, packArrayTo, unpackArrayTo, |
37
|
|
|
pack, packStringTo, packTo, packString} from './vendor/byte-data.js'; |
38
|
|
|
import {wavHeader, validateHeader_, AUDIO_FORMATS} from './lib/wavheader.js'; |
|
|
|
|
39
|
|
|
import {riffChunks, findChunk_} from './vendor/riff-chunks.js'; |
40
|
|
|
import BufferIO from './lib/bufferio.js'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Class representing a wav file. |
44
|
|
|
* @ignore |
45
|
|
|
*/ |
46
|
|
|
export default class WaveFile { |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param {?Uint8Array} bytes A wave file buffer. |
50
|
|
|
* @throws {Error} If no 'RIFF' chunk is found. |
51
|
|
|
* @throws {Error} If no 'fmt ' chunk is found. |
52
|
|
|
* @throws {Error} If no 'data' chunk is found. |
53
|
|
|
*/ |
54
|
|
|
constructor(bytes=null) { |
55
|
|
|
/** |
56
|
|
|
* The container identifier. |
57
|
|
|
* 'RIFF', 'RIFX' and 'RF64' are supported. |
58
|
|
|
* @type {string} |
59
|
|
|
*/ |
60
|
|
|
this.container = ''; |
61
|
|
|
/** |
62
|
|
|
* @type {number} |
63
|
|
|
*/ |
64
|
|
|
this.chunkSize = 0; |
65
|
|
|
/** |
66
|
|
|
* The format. |
67
|
|
|
* Always 'WAVE'. |
68
|
|
|
* @type {string} |
69
|
|
|
*/ |
70
|
|
|
this.format = ''; |
71
|
|
|
/** |
72
|
|
|
* The data of the 'fmt' chunk. |
73
|
|
|
* @type {!Object<string, *>} |
74
|
|
|
*/ |
75
|
|
|
this.fmt = { |
76
|
|
|
/** @type {string} */ |
77
|
|
|
chunkId: '', |
78
|
|
|
/** @type {number} */ |
79
|
|
|
chunkSize: 0, |
80
|
|
|
/** @type {number} */ |
81
|
|
|
audioFormat: 0, |
82
|
|
|
/** @type {number} */ |
83
|
|
|
numChannels: 0, |
84
|
|
|
/** @type {number} */ |
85
|
|
|
sampleRate: 0, |
86
|
|
|
/** @type {number} */ |
87
|
|
|
byteRate: 0, |
88
|
|
|
/** @type {number} */ |
89
|
|
|
blockAlign: 0, |
90
|
|
|
/** @type {number} */ |
91
|
|
|
bitsPerSample: 0, |
92
|
|
|
/** @type {number} */ |
93
|
|
|
cbSize: 0, |
94
|
|
|
/** @type {number} */ |
95
|
|
|
validBitsPerSample: 0, |
96
|
|
|
/** @type {number} */ |
97
|
|
|
dwChannelMask: 0, |
98
|
|
|
/** |
99
|
|
|
* 4 32-bit values representing a 128-bit ID |
100
|
|
|
* @type {!Array<number>} |
101
|
|
|
*/ |
102
|
|
|
subformat: [] |
103
|
|
|
}; |
104
|
|
|
/** |
105
|
|
|
* The data of the 'fact' chunk. |
106
|
|
|
* @type {!Object<string, *>} |
107
|
|
|
*/ |
108
|
|
|
this.fact = { |
109
|
|
|
/** @type {string} */ |
110
|
|
|
chunkId: '', |
111
|
|
|
/** @type {number} */ |
112
|
|
|
chunkSize: 0, |
113
|
|
|
/** @type {number} */ |
114
|
|
|
dwSampleLength: 0 |
115
|
|
|
}; |
116
|
|
|
/** |
117
|
|
|
* The data of the 'cue ' chunk. |
118
|
|
|
* @type {!Object<string, *>} |
119
|
|
|
*/ |
120
|
|
|
this.cue = { |
121
|
|
|
/** @type {string} */ |
122
|
|
|
chunkId: '', |
123
|
|
|
/** @type {number} */ |
124
|
|
|
chunkSize: 0, |
125
|
|
|
/** @type {number} */ |
126
|
|
|
dwCuePoints: 0, |
127
|
|
|
/** @type {!Array<!Object>} */ |
128
|
|
|
points: [], |
129
|
|
|
}; |
130
|
|
|
/** |
131
|
|
|
* The data of the 'smpl' chunk. |
132
|
|
|
* @type {!Object<string, *>} |
133
|
|
|
*/ |
134
|
|
|
this.smpl = { |
135
|
|
|
/** @type {string} */ |
136
|
|
|
chunkId: '', |
137
|
|
|
/** @type {number} */ |
138
|
|
|
chunkSize: 0, |
139
|
|
|
/** @type {number} */ |
140
|
|
|
dwManufacturer: 0, |
141
|
|
|
/** @type {number} */ |
142
|
|
|
dwProduct: 0, |
143
|
|
|
/** @type {number} */ |
144
|
|
|
dwSamplePeriod: 0, |
145
|
|
|
/** @type {number} */ |
146
|
|
|
dwMIDIUnityNote: 0, |
147
|
|
|
/** @type {number} */ |
148
|
|
|
dwMIDIPitchFraction: 0, |
149
|
|
|
/** @type {number} */ |
150
|
|
|
dwSMPTEFormat: 0, |
151
|
|
|
/** @type {number} */ |
152
|
|
|
dwSMPTEOffset: 0, |
153
|
|
|
/** @type {number} */ |
154
|
|
|
dwNumSampleLoops: 0, |
155
|
|
|
/** @type {number} */ |
156
|
|
|
dwSamplerData: 0, |
157
|
|
|
/** @type {!Array<!Object>} */ |
158
|
|
|
loops: [] |
159
|
|
|
}; |
160
|
|
|
/** |
161
|
|
|
* The data of the 'bext' chunk. |
162
|
|
|
* @type {!Object<string, *>} |
163
|
|
|
*/ |
164
|
|
|
this.bext = { |
165
|
|
|
/** @type {string} */ |
166
|
|
|
chunkId: '', |
167
|
|
|
/** @type {number} */ |
168
|
|
|
chunkSize: 0, |
169
|
|
|
/** @type {string} */ |
170
|
|
|
description: '', //256 |
171
|
|
|
/** @type {string} */ |
172
|
|
|
originator: '', //32 |
173
|
|
|
/** @type {string} */ |
174
|
|
|
originatorReference: '', //32 |
175
|
|
|
/** @type {string} */ |
176
|
|
|
originationDate: '', //10 |
177
|
|
|
/** @type {string} */ |
178
|
|
|
originationTime: '', //8 |
179
|
|
|
/** |
180
|
|
|
* 2 32-bit values, timeReference high and low |
181
|
|
|
* @type {!Array<number>} |
182
|
|
|
*/ |
183
|
|
|
timeReference: [0, 0], |
184
|
|
|
/** @type {number} */ |
185
|
|
|
version: 0, //WORD |
186
|
|
|
/** @type {string} */ |
187
|
|
|
UMID: '', // 64 chars |
188
|
|
|
/** @type {number} */ |
189
|
|
|
loudnessValue: 0, //WORD |
190
|
|
|
/** @type {number} */ |
191
|
|
|
loudnessRange: 0, //WORD |
192
|
|
|
/** @type {number} */ |
193
|
|
|
maxTruePeakLevel: 0, //WORD |
194
|
|
|
/** @type {number} */ |
195
|
|
|
maxMomentaryLoudness: 0, //WORD |
196
|
|
|
/** @type {number} */ |
197
|
|
|
maxShortTermLoudness: 0, //WORD |
198
|
|
|
/** @type {string} */ |
199
|
|
|
reserved: '', //180 |
200
|
|
|
/** @type {string} */ |
201
|
|
|
codingHistory: '' // string, unlimited |
202
|
|
|
}; |
203
|
|
|
/** |
204
|
|
|
* The data of the 'ds64' chunk. |
205
|
|
|
* Used only with RF64 files. |
206
|
|
|
* @type {!Object<string, *>} |
207
|
|
|
*/ |
208
|
|
|
this.ds64 = { |
209
|
|
|
/** @type {string} */ |
210
|
|
|
chunkId: '', |
211
|
|
|
/** @type {number} */ |
212
|
|
|
chunkSize: 0, |
213
|
|
|
/** @type {number} */ |
214
|
|
|
riffSizeHigh: 0, // DWORD |
215
|
|
|
/** @type {number} */ |
216
|
|
|
riffSizeLow: 0, // DWORD |
217
|
|
|
/** @type {number} */ |
218
|
|
|
dataSizeHigh: 0, // DWORD |
219
|
|
|
/** @type {number} */ |
220
|
|
|
dataSizeLow: 0, // DWORD |
221
|
|
|
/** @type {number} */ |
222
|
|
|
originationTime: 0, // DWORD |
223
|
|
|
/** @type {number} */ |
224
|
|
|
sampleCountHigh: 0, // DWORD |
225
|
|
|
/** @type {number} */ |
226
|
|
|
sampleCountLow: 0 // DWORD |
227
|
|
|
/** @type {number} */ |
228
|
|
|
//'tableLength': 0, // DWORD |
229
|
|
|
/** @type {!Array<number>} */ |
230
|
|
|
//'table': [] |
231
|
|
|
}; |
232
|
|
|
/** |
233
|
|
|
* The data of the 'data' chunk. |
234
|
|
|
* @type {!Object<string, *>} |
235
|
|
|
*/ |
236
|
|
|
this.data = { |
237
|
|
|
/** @type {string} */ |
238
|
|
|
chunkId: '', |
239
|
|
|
/** @type {number} */ |
240
|
|
|
chunkSize: 0, |
241
|
|
|
/** @type {!Uint8Array} */ |
242
|
|
|
samples: new Uint8Array(0) |
243
|
|
|
}; |
244
|
|
|
/** |
245
|
|
|
* The data of the 'LIST' chunks. |
246
|
|
|
* Each item in this list look like this: |
247
|
|
|
* { |
248
|
|
|
* chunkId: '', |
249
|
|
|
* chunkSize: 0, |
250
|
|
|
* format: '', |
251
|
|
|
* subChunks: [] |
252
|
|
|
* } |
253
|
|
|
* @type {!Array<!Object>} |
254
|
|
|
*/ |
255
|
|
|
this.LIST = []; |
256
|
|
|
/** |
257
|
|
|
* The data of the 'junk' chunk. |
258
|
|
|
* @type {!Object<string, *>} |
259
|
|
|
*/ |
260
|
|
|
this.junk = { |
261
|
|
|
/** @type {string} */ |
262
|
|
|
chunkId: '', |
263
|
|
|
/** @type {number} */ |
264
|
|
|
chunkSize: 0, |
265
|
|
|
/** @type {!Array<number>} */ |
266
|
|
|
chunkData: [] |
267
|
|
|
}; |
268
|
|
|
/** |
269
|
|
|
* @type {!Object} |
270
|
|
|
* @private |
271
|
|
|
*/ |
272
|
|
|
this.uInt16_ = {bits: 16, be: false}; |
273
|
|
|
/** |
274
|
|
|
* @type {!Object} |
275
|
|
|
* @private |
276
|
|
|
*/ |
277
|
|
|
this.uInt32_ = {bits: 32, be: false}; |
278
|
|
|
/** |
279
|
|
|
* The bit depth code according to the samples. |
280
|
|
|
* @type {string} |
281
|
|
|
*/ |
282
|
|
|
this.bitDepth = '0'; |
283
|
|
|
/** |
284
|
|
|
* @type {!Object} |
285
|
|
|
* @private |
286
|
|
|
*/ |
287
|
|
|
this.dataType = {}; |
288
|
|
|
this.io = new BufferIO(); |
289
|
|
|
// Load a file from the buffer if one was passed |
290
|
|
|
// when creating the object |
291
|
|
|
if(bytes) { |
292
|
|
|
this.fromBuffer(bytes); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Set up the WaveFile object based on the arguments passed. |
298
|
|
|
* @param {number} numChannels The number of channels |
299
|
|
|
* (Integer numbers: 1 for mono, 2 stereo and so on). |
300
|
|
|
* @param {number} sampleRate The sample rate. |
301
|
|
|
* Integer numbers like 8000, 44100, 48000, 96000, 192000. |
302
|
|
|
* @param {string} bitDepthCode The audio bit depth code. |
303
|
|
|
* One of '4', '8', '8a', '8m', '16', '24', '32', '32f', '64' |
304
|
|
|
* or any value between '8' and '32' (like '12'). |
305
|
|
|
* @param {!Array<number>|!Array<!Array<number>>|!ArrayBufferView} samples |
306
|
|
|
* The samples. Must be in the correct range according to the bit depth. |
307
|
|
|
* @param {?Object} options Optional. Used to force the container |
308
|
|
|
* as RIFX with {'container': 'RIFX'} |
309
|
|
|
* @throws {Error} If any argument does not meet the criteria. |
310
|
|
|
*/ |
311
|
|
|
fromScratch(numChannels, sampleRate, bitDepthCode, samples, options={}) { |
312
|
|
|
if (!options.container) { |
313
|
|
|
options.container = 'RIFF'; |
314
|
|
|
} |
315
|
|
|
this.container = options.container; |
316
|
|
|
this.bitDepth = bitDepthCode; |
317
|
|
|
samples = this.interleave_(samples); |
318
|
|
|
this.updateDataType_(); |
319
|
|
|
/** @type {number} */ |
320
|
|
|
let numBytes = this.dataType.bits / 8; |
321
|
|
|
this.data.samples = new Uint8Array(samples.length * numBytes); |
322
|
|
|
packArrayTo(samples, this.dataType, this.data.samples); |
323
|
|
|
/** @type {!Object} */ |
324
|
|
|
let header = wavHeader( |
325
|
|
|
bitDepthCode, numChannels, sampleRate, |
326
|
|
|
numBytes, this.data.samples.length, options); |
327
|
|
|
this.clearHeader_(); |
328
|
|
|
this.chunkSize = header.chunkSize; |
329
|
|
|
this.format = header.format; |
330
|
|
|
this.fmt = header.fmt; |
331
|
|
|
if (header.fact) { |
332
|
|
|
this.fact = header.fact; |
333
|
|
|
} |
334
|
|
|
this.data.chunkId = 'data'; |
335
|
|
|
this.data.chunkSize = this.data.samples.length; |
336
|
|
|
validateHeader_(this); |
337
|
|
|
this.LEorBE_(); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Set up the WaveFile object from a byte buffer. |
342
|
|
|
* @param {!Uint8Array} bytes The buffer. |
343
|
|
|
* @param {boolean=} samples True if the samples should be loaded. |
344
|
|
|
* @throws {Error} If container is not RIFF, RIFX or RF64. |
345
|
|
|
* @throws {Error} If no 'fmt ' chunk is found. |
346
|
|
|
* @throws {Error} If no 'data' chunk is found. |
347
|
|
|
*/ |
348
|
|
|
fromBuffer(bytes, samples=true) { |
349
|
|
|
this.readWavBuffer(bytes, samples); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Return a byte buffer representig the WaveFile object as a .wav file. |
354
|
|
|
* The return value of this method can be written straight to disk. |
355
|
|
|
* @return {!Uint8Array} A .wav file. |
356
|
|
|
* @throws {Error} If any property of the object appears invalid. |
357
|
|
|
*/ |
358
|
|
|
toBuffer() { |
359
|
|
|
validateHeader_(this); |
360
|
|
|
return this.createWaveFile_(); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Use a .wav file encoded as a base64 string to load the WaveFile object. |
365
|
|
|
* @param {string} base64String A .wav file as a base64 string. |
366
|
|
|
* @throws {Error} If any property of the object appears invalid. |
367
|
|
|
*/ |
368
|
|
|
fromBase64(base64String) { |
369
|
|
|
this.fromBuffer(new Uint8Array(decode(base64String))); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Return a base64 string representig the WaveFile object as a .wav file. |
374
|
|
|
* @return {string} A .wav file as a base64 string. |
375
|
|
|
* @throws {Error} If any property of the object appears invalid. |
376
|
|
|
*/ |
377
|
|
|
toBase64() { |
378
|
|
|
/** @type {!Uint8Array} */ |
379
|
|
|
let buffer = this.toBuffer(); |
380
|
|
|
return encode(buffer, 0, buffer.length); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Return a DataURI string representig the WaveFile object as a .wav file. |
385
|
|
|
* The return of this method can be used to load the audio in browsers. |
386
|
|
|
* @return {string} A .wav file as a DataURI. |
387
|
|
|
* @throws {Error} If any property of the object appears invalid. |
388
|
|
|
*/ |
389
|
|
|
toDataURI() { |
390
|
|
|
return 'data:audio/wav;base64,' + this.toBase64(); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Use a .wav file encoded as a DataURI to load the WaveFile object. |
395
|
|
|
* @param {string} dataURI A .wav file as DataURI. |
396
|
|
|
* @throws {Error} If any property of the object appears invalid. |
397
|
|
|
*/ |
398
|
|
|
fromDataURI(dataURI) { |
399
|
|
|
this.fromBase64(dataURI.replace('data:audio/wav;base64,', '')); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Force a file as RIFF. |
404
|
|
|
*/ |
405
|
|
|
toRIFF() { |
406
|
|
|
if (this.container == 'RF64') { |
407
|
|
|
this.fromScratch( |
408
|
|
|
this.fmt.numChannels, |
409
|
|
|
this.fmt.sampleRate, |
410
|
|
|
this.bitDepth, |
411
|
|
|
unpackArray(this.data.samples, this.dataType)); |
412
|
|
|
} else { |
413
|
|
|
this.dataType.be = true; |
414
|
|
|
this.fromScratch( |
415
|
|
|
this.fmt.numChannels, |
416
|
|
|
this.fmt.sampleRate, |
417
|
|
|
this.bitDepth, |
418
|
|
|
unpackArray(this.data.samples, this.dataType)); |
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Force a file as RIFX. |
424
|
|
|
*/ |
425
|
|
|
toRIFX() { |
426
|
|
|
if (this.container == 'RF64') { |
427
|
|
|
this.fromScratch( |
428
|
|
|
this.fmt.numChannels, |
429
|
|
|
this.fmt.sampleRate, |
430
|
|
|
this.bitDepth, |
431
|
|
|
unpackArray(this.data.samples, this.dataType), |
432
|
|
|
{container: 'RIFX'}); |
433
|
|
|
} else { |
434
|
|
|
this.fromScratch( |
435
|
|
|
this.fmt.numChannels, |
436
|
|
|
this.fmt.sampleRate, |
437
|
|
|
this.bitDepth, |
438
|
|
|
unpackArray(this.data.samples, this.dataType), |
439
|
|
|
{container: 'RIFX'}); |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Change the bit depth of the samples. |
445
|
|
|
* @param {string} newBitDepth The new bit depth of the samples. |
446
|
|
|
* One of '8' ... '32' (integers), '32f' or '64' (floats) |
447
|
|
|
* @param {boolean} changeResolution A boolean indicating if the |
448
|
|
|
* resolution of samples should be actually changed or not. |
449
|
|
|
* @throws {Error} If the bit depth is not valid. |
450
|
|
|
*/ |
451
|
|
|
toBitDepth(newBitDepth, changeResolution=true) { |
452
|
|
|
// @type {string} |
453
|
|
|
let toBitDepth = newBitDepth; |
454
|
|
|
// @type {string} |
455
|
|
|
let thisBitDepth = this.bitDepth; |
456
|
|
|
if (!changeResolution) { |
457
|
|
|
if (newBitDepth != '32f') { |
458
|
|
|
toBitDepth = this.dataType.bits.toString(); |
459
|
|
|
} |
460
|
|
|
thisBitDepth = this.dataType.bits; |
461
|
|
|
} |
462
|
|
|
this.assureUncompressed_(); |
463
|
|
|
// @type {number} |
464
|
|
|
let sampleCount = this.data.samples.length / (this.dataType.bits / 8); |
465
|
|
|
// @type {!Float64Array} |
466
|
|
|
let typedSamplesInput = new Float64Array(sampleCount + 1); |
467
|
|
|
// @type {!Float64Array} |
468
|
|
|
let typedSamplesOutput = new Float64Array(sampleCount + 1); |
469
|
|
|
unpackArrayTo(this.data.samples, this.dataType, typedSamplesInput); |
470
|
|
|
bitDepthLib( |
471
|
|
|
typedSamplesInput, thisBitDepth, toBitDepth, typedSamplesOutput); |
472
|
|
|
this.fromScratch( |
473
|
|
|
this.fmt.numChannels, |
474
|
|
|
this.fmt.sampleRate, |
475
|
|
|
newBitDepth, |
476
|
|
|
typedSamplesOutput, |
477
|
|
|
{container: this.correctContainer_()}); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Encode a 16-bit wave file as 4-bit IMA ADPCM. |
482
|
|
|
* @throws {Error} If sample rate is not 8000. |
483
|
|
|
* @throws {Error} If number of channels is not 1. |
484
|
|
|
*/ |
485
|
|
|
toIMAADPCM() { |
486
|
|
|
if (this.fmt.sampleRate !== 8000) { |
487
|
|
|
throw new Error( |
488
|
|
|
'Only 8000 Hz files can be compressed as IMA-ADPCM.'); |
489
|
|
|
} else if(this.fmt.numChannels !== 1) { |
490
|
|
|
throw new Error( |
491
|
|
|
'Only mono files can be compressed as IMA-ADPCM.'); |
492
|
|
|
} else { |
493
|
|
|
this.assure16Bit_(); |
494
|
|
|
let output = new Int16Array(this.data.samples.length / 2); |
495
|
|
|
unpackArrayTo(this.data.samples, this.dataType, output); |
496
|
|
|
this.fromScratch( |
497
|
|
|
this.fmt.numChannels, |
498
|
|
|
this.fmt.sampleRate, |
499
|
|
|
'4', |
500
|
|
|
imaadpcm.encode(output), |
501
|
|
|
{container: this.correctContainer_()}); |
502
|
|
|
} |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* Decode a 4-bit IMA ADPCM wave file as a 16-bit wave file. |
507
|
|
|
* @param {string} bitDepthCode The new bit depth of the samples. |
508
|
|
|
* One of '8' ... '32' (integers), '32f' or '64' (floats). |
509
|
|
|
* Optional. Default is 16. |
510
|
|
|
*/ |
511
|
|
|
fromIMAADPCM(bitDepthCode='16') { |
512
|
|
|
this.fromScratch( |
513
|
|
|
this.fmt.numChannels, |
514
|
|
|
this.fmt.sampleRate, |
515
|
|
|
'16', |
516
|
|
|
imaadpcm.decode(this.data.samples, this.fmt.blockAlign), |
517
|
|
|
{container: this.correctContainer_()}); |
518
|
|
|
if (bitDepthCode != '16') { |
519
|
|
|
this.toBitDepth(bitDepthCode); |
520
|
|
|
} |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* Encode a 16-bit wave file as 8-bit A-Law. |
525
|
|
|
*/ |
526
|
|
|
toALaw() { |
527
|
|
|
this.assure16Bit_(); |
528
|
|
|
let output = new Int16Array(this.data.samples.length / 2); |
529
|
|
|
unpackArrayTo(this.data.samples, this.dataType, output); |
530
|
|
|
this.fromScratch( |
531
|
|
|
this.fmt.numChannels, |
532
|
|
|
this.fmt.sampleRate, |
533
|
|
|
'8a', |
534
|
|
|
alawmulaw.alaw.encode(output), |
535
|
|
|
{container: this.correctContainer_()}); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* Decode a 8-bit A-Law wave file into a 16-bit wave file. |
540
|
|
|
* @param {string} bitDepthCode The new bit depth of the samples. |
541
|
|
|
* One of '8' ... '32' (integers), '32f' or '64' (floats). |
542
|
|
|
* Optional. Default is 16. |
543
|
|
|
*/ |
544
|
|
|
fromALaw(bitDepthCode='16') { |
545
|
|
|
this.fromScratch( |
546
|
|
|
this.fmt.numChannels, |
547
|
|
|
this.fmt.sampleRate, |
548
|
|
|
'16', |
549
|
|
|
alawmulaw.alaw.decode(this.data.samples), |
550
|
|
|
{container: this.correctContainer_()}); |
551
|
|
|
if (bitDepthCode != '16') { |
552
|
|
|
this.toBitDepth(bitDepthCode); |
553
|
|
|
} |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* Encode 16-bit wave file as 8-bit mu-Law. |
558
|
|
|
*/ |
559
|
|
|
toMuLaw() { |
560
|
|
|
this.assure16Bit_(); |
561
|
|
|
let output = new Int16Array(this.data.samples.length / 2); |
562
|
|
|
unpackArrayTo(this.data.samples, this.dataType, output); |
563
|
|
|
this.fromScratch( |
564
|
|
|
this.fmt.numChannels, |
565
|
|
|
this.fmt.sampleRate, |
566
|
|
|
'8m', |
567
|
|
|
alawmulaw.mulaw.encode(output), |
568
|
|
|
{container: this.correctContainer_()}); |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Decode a 8-bit mu-Law wave file into a 16-bit wave file. |
573
|
|
|
* @param {string} bitDepthCode The new bit depth of the samples. |
574
|
|
|
* One of '8' ... '32' (integers), '32f' or '64' (floats). |
575
|
|
|
* Optional. Default is 16. |
576
|
|
|
*/ |
577
|
|
|
fromMuLaw(bitDepthCode='16') { |
578
|
|
|
this.fromScratch( |
579
|
|
|
this.fmt.numChannels, |
580
|
|
|
this.fmt.sampleRate, |
581
|
|
|
'16', |
582
|
|
|
alawmulaw.mulaw.decode(this.data.samples), |
583
|
|
|
{container: this.correctContainer_()}); |
584
|
|
|
if (bitDepthCode != '16') { |
585
|
|
|
this.toBitDepth(bitDepthCode); |
586
|
|
|
} |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* Write a RIFF tag in the INFO chunk. If the tag do not exist, |
591
|
|
|
* then it is created. It if exists, it is overwritten. |
592
|
|
|
* @param {string} tag The tag name. |
593
|
|
|
* @param {string} value The tag value. |
594
|
|
|
* @throws {Error} If the tag name is not valid. |
595
|
|
|
*/ |
596
|
|
|
setTag(tag, value) { |
597
|
|
|
tag = this.fixTagName_(tag); |
598
|
|
|
/** @type {!Object} */ |
599
|
|
|
let index = this.getTagIndex_(tag); |
600
|
|
|
if (index.TAG !== null) { |
601
|
|
|
this.LIST[index.LIST].subChunks[index.TAG].chunkSize = |
602
|
|
|
value.length + 1; |
603
|
|
|
this.LIST[index.LIST].subChunks[index.TAG].value = value; |
604
|
|
|
} else if (index.LIST !== null) { |
605
|
|
|
this.LIST[index.LIST].subChunks.push({ |
606
|
|
|
chunkId: tag, |
607
|
|
|
chunkSize: value.length + 1, |
608
|
|
|
value: value}); |
609
|
|
|
} else { |
610
|
|
|
this.LIST.push({ |
611
|
|
|
chunkId: 'LIST', |
612
|
|
|
chunkSize: 8 + value.length + 1, |
613
|
|
|
format: 'INFO', |
614
|
|
|
subChunks: []}); |
615
|
|
|
this.LIST[this.LIST.length - 1].subChunks.push({ |
616
|
|
|
chunkId: tag, |
617
|
|
|
chunkSize: value.length + 1, |
618
|
|
|
value: value}); |
619
|
|
|
} |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* Return the value of a RIFF tag in the INFO chunk. |
624
|
|
|
* @param {string} tag The tag name. |
625
|
|
|
* @return {?string} The value if the tag is found, null otherwise. |
626
|
|
|
*/ |
627
|
|
|
getTag(tag) { |
628
|
|
|
/** @type {!Object} */ |
629
|
|
|
let index = this.getTagIndex_(tag); |
630
|
|
|
if (index.TAG !== null) { |
631
|
|
|
return this.LIST[index.LIST].subChunks[index.TAG].value; |
632
|
|
|
} |
633
|
|
|
return null; |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
/** |
637
|
|
|
* Remove a RIFF tag in the INFO chunk. |
638
|
|
|
* @param {string} tag The tag name. |
639
|
|
|
* @return {boolean} True if a tag was deleted. |
640
|
|
|
*/ |
641
|
|
|
deleteTag(tag) { |
642
|
|
|
/** @type {!Object} */ |
643
|
|
|
let index = this.getTagIndex_(tag); |
644
|
|
|
if (index.TAG !== null) { |
645
|
|
|
this.LIST[index.LIST].subChunks.splice(index.TAG, 1); |
646
|
|
|
return true; |
647
|
|
|
} |
648
|
|
|
return false; |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
/** |
652
|
|
|
* Create a cue point in the wave file. |
653
|
|
|
* @param {number} position The cue point position in milliseconds. |
654
|
|
|
* @param {string} labl The LIST adtl labl text of the marker. Optional. |
655
|
|
|
*/ |
656
|
|
|
setCuePoint(position, labl='') { |
657
|
|
|
this.cue.chunkId = 'cue '; |
658
|
|
|
position = (position * this.fmt.sampleRate) / 1000; |
659
|
|
|
/** @type {!Array<!Object>} */ |
660
|
|
|
let existingPoints = this.getCuePoints_(); |
661
|
|
|
this.clearLISTadtl_(); |
662
|
|
|
/** @type {number} */ |
663
|
|
|
let len = this.cue.points.length; |
664
|
|
|
this.cue.points = []; |
665
|
|
|
/** @type {boolean} */ |
666
|
|
|
let hasSet = false; |
667
|
|
|
if (len === 0) { |
668
|
|
|
this.setCuePoint_(position, 1, labl); |
669
|
|
|
} else { |
670
|
|
|
for (let i=0; i<len; i++) { |
671
|
|
|
if (existingPoints[i].dwPosition > position && !hasSet) { |
672
|
|
|
this.setCuePoint_(position, i + 1, labl); |
673
|
|
|
this.setCuePoint_( |
674
|
|
|
existingPoints[i].dwPosition, |
675
|
|
|
i + 2, |
676
|
|
|
existingPoints[i].label); |
677
|
|
|
hasSet = true; |
678
|
|
|
} else { |
679
|
|
|
this.setCuePoint_( |
680
|
|
|
existingPoints[i].dwPosition, |
681
|
|
|
i + 1, |
682
|
|
|
existingPoints[i].label); |
683
|
|
|
} |
684
|
|
|
} |
685
|
|
|
if (!hasSet) { |
686
|
|
|
this.setCuePoint_(position, this.cue.points.length + 1, labl); |
687
|
|
|
} |
688
|
|
|
} |
689
|
|
|
this.cue.dwCuePoints = this.cue.points.length; |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
/** |
693
|
|
|
* Remove a cue point from a wave file. |
694
|
|
|
* @param {number} index the index of the point. First is 1, |
695
|
|
|
* second is 2, and so on. |
696
|
|
|
*/ |
697
|
|
|
deleteCuePoint(index) { |
698
|
|
|
this.cue.chunkId = 'cue '; |
699
|
|
|
/** @type {!Array<!Object>} */ |
700
|
|
|
let existingPoints = this.getCuePoints_(); |
701
|
|
|
this.clearLISTadtl_(); |
702
|
|
|
/** @type {number} */ |
703
|
|
|
let len = this.cue.points.length; |
704
|
|
|
this.cue.points = []; |
705
|
|
|
for (let i=0; i<len; i++) { |
706
|
|
|
if (i + 1 !== index) { |
707
|
|
|
this.setCuePoint_( |
708
|
|
|
existingPoints[i].dwPosition, |
709
|
|
|
i + 1, |
710
|
|
|
existingPoints[i].label); |
711
|
|
|
} |
712
|
|
|
} |
713
|
|
|
this.cue.dwCuePoints = this.cue.points.length; |
714
|
|
|
if (this.cue.dwCuePoints) { |
715
|
|
|
this.cue.chunkId = 'cue '; |
716
|
|
|
} else { |
717
|
|
|
this.cue.chunkId = ''; |
718
|
|
|
this.clearLISTadtl_(); |
719
|
|
|
} |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* Update the label of a cue point. |
724
|
|
|
* @param {number} pointIndex The ID of the cue point. |
725
|
|
|
* @param {string} label The new text for the label. |
726
|
|
|
*/ |
727
|
|
|
updateLabel(pointIndex, label) { |
728
|
|
|
/** @type {?number} */ |
729
|
|
|
let adtlIndex = this.getAdtlChunk_(); |
730
|
|
|
if (adtlIndex !== null) { |
731
|
|
|
for (let i=0; i<this.LIST[adtlIndex].subChunks.length; i++) { |
732
|
|
|
if (this.LIST[adtlIndex].subChunks[i].dwName == |
733
|
|
|
pointIndex) { |
734
|
|
|
this.LIST[adtlIndex].subChunks[i].value = label; |
735
|
|
|
} |
736
|
|
|
} |
737
|
|
|
} |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
/** |
741
|
|
|
* Make the file 16-bit if it is not. |
742
|
|
|
* @private |
743
|
|
|
*/ |
744
|
|
|
assure16Bit_() { |
745
|
|
|
this.assureUncompressed_(); |
746
|
|
|
if (this.bitDepth != '16') { |
747
|
|
|
this.toBitDepth('16'); |
748
|
|
|
} |
749
|
|
|
} |
750
|
|
|
|
751
|
|
|
/** |
752
|
|
|
* Uncompress the samples in case of a compressed file. |
753
|
|
|
* @private |
754
|
|
|
*/ |
755
|
|
|
assureUncompressed_() { |
756
|
|
|
if (this.bitDepth == '8a') { |
757
|
|
|
this.fromALaw(); |
758
|
|
|
} else if(this.bitDepth == '8m') { |
759
|
|
|
this.fromMuLaw(); |
760
|
|
|
} else if (this.bitDepth == '4') { |
761
|
|
|
this.fromIMAADPCM(); |
762
|
|
|
} |
763
|
|
|
} |
764
|
|
|
|
765
|
|
|
/** |
766
|
|
|
* Set up the WaveFile object from a byte buffer. |
767
|
|
|
* @param {!Array<number>|!Array<!Array<number>>|!ArrayBufferView} samples The samples. |
768
|
|
|
* @private |
769
|
|
|
*/ |
770
|
|
|
interleave_(samples) { |
771
|
|
|
if (samples.length > 0) { |
772
|
|
|
if (samples[0].constructor === Array) { |
773
|
|
|
/** @type {!Array<number>} */ |
774
|
|
|
let finalSamples = []; |
775
|
|
|
for (let i=0; i < samples[0].length; i++) { |
776
|
|
|
for (let j=0; j < samples.length; j++) { |
777
|
|
|
finalSamples.push(samples[j][i]); |
778
|
|
|
} |
779
|
|
|
} |
780
|
|
|
samples = finalSamples; |
781
|
|
|
} |
782
|
|
|
} |
783
|
|
|
return samples; |
784
|
|
|
} |
785
|
|
|
|
786
|
|
|
/** |
787
|
|
|
* Push a new cue point in this.cue.points. |
788
|
|
|
* @param {number} position The position in milliseconds. |
789
|
|
|
* @param {number} dwName the dwName of the cue point |
790
|
|
|
* @private |
791
|
|
|
*/ |
792
|
|
|
setCuePoint_(position, dwName, label) { |
793
|
|
|
this.cue.points.push({ |
794
|
|
|
dwName: dwName, |
795
|
|
|
dwPosition: position, |
796
|
|
|
fccChunk: 'data', |
797
|
|
|
dwChunkStart: 0, |
798
|
|
|
dwBlockStart: 0, |
799
|
|
|
dwSampleOffset: position, |
800
|
|
|
}); |
801
|
|
|
this.setLabl_(dwName, label); |
802
|
|
|
} |
803
|
|
|
|
804
|
|
|
/** |
805
|
|
|
* Return an array with the position of all cue points in the file. |
806
|
|
|
* @return {!Array<!Object>} |
807
|
|
|
* @private |
808
|
|
|
*/ |
809
|
|
|
getCuePoints_() { |
810
|
|
|
/** @type {!Array<!Object>} */ |
811
|
|
|
let points = []; |
812
|
|
|
for (let i=0; i<this.cue.points.length; i++) { |
813
|
|
|
points.push({ |
814
|
|
|
dwPosition: this.cue.points[i].dwPosition, |
815
|
|
|
label: this.getLabelForCuePoint_( |
816
|
|
|
this.cue.points[i].dwName)}); |
817
|
|
|
} |
818
|
|
|
return points; |
819
|
|
|
} |
820
|
|
|
|
821
|
|
|
/** |
822
|
|
|
* Return the label of a cue point. |
823
|
|
|
* @param {number} pointDwName The ID of the cue point. |
824
|
|
|
* @return {string} |
825
|
|
|
* @private |
826
|
|
|
*/ |
827
|
|
|
getLabelForCuePoint_(pointDwName) { |
828
|
|
|
/** @type {?number} */ |
829
|
|
|
let adtlIndex = this.getAdtlChunk_(); |
830
|
|
|
if (adtlIndex !== null) { |
831
|
|
|
for (let i=0; i<this.LIST[adtlIndex].subChunks.length; i++) { |
832
|
|
|
if (this.LIST[adtlIndex].subChunks[i].dwName == |
833
|
|
|
pointDwName) { |
834
|
|
|
return this.LIST[adtlIndex].subChunks[i].value; |
835
|
|
|
} |
836
|
|
|
} |
837
|
|
|
} |
838
|
|
|
return ''; |
839
|
|
|
} |
840
|
|
|
|
841
|
|
|
/** |
842
|
|
|
* Clear any LIST chunk labeled as 'adtl'. |
843
|
|
|
* @private |
844
|
|
|
*/ |
845
|
|
|
clearLISTadtl_() { |
846
|
|
|
for (let i=0; i<this.LIST.length; i++) { |
847
|
|
|
if (this.LIST[i].format == 'adtl') { |
848
|
|
|
this.LIST.splice(i); |
849
|
|
|
} |
850
|
|
|
} |
851
|
|
|
} |
852
|
|
|
|
853
|
|
|
/** |
854
|
|
|
* Create a new 'labl' subchunk in a 'LIST' chunk of type 'adtl'. |
855
|
|
|
* @param {number} dwName The ID of the cue point. |
856
|
|
|
* @param {string} label The label for the cue point. |
857
|
|
|
* @private |
858
|
|
|
*/ |
859
|
|
|
setLabl_(dwName, label) { |
860
|
|
|
/** @type {?number} */ |
861
|
|
|
let adtlIndex = this.getAdtlChunk_(); |
862
|
|
|
if (adtlIndex === null) { |
863
|
|
|
this.LIST.push({ |
864
|
|
|
chunkId: 'LIST', |
865
|
|
|
chunkSize: 4, |
866
|
|
|
format: 'adtl', |
867
|
|
|
subChunks: []}); |
868
|
|
|
adtlIndex = this.LIST.length - 1; |
869
|
|
|
} |
870
|
|
|
this.setLabelText_(adtlIndex === null ? 0 : adtlIndex, dwName, label); |
871
|
|
|
} |
872
|
|
|
|
873
|
|
|
/** |
874
|
|
|
* Create a new 'labl' subchunk in a 'LIST' chunk of type 'adtl'. |
875
|
|
|
* @param {number} adtlIndex The index of the 'adtl' LIST in this.LIST. |
876
|
|
|
* @param {number} dwName The ID of the cue point. |
877
|
|
|
* @param {string} label The label for the cue point. |
878
|
|
|
* @private |
879
|
|
|
*/ |
880
|
|
|
setLabelText_(adtlIndex, dwName, label) { |
881
|
|
|
this.LIST[adtlIndex].subChunks.push({ |
882
|
|
|
chunkId: 'labl', |
883
|
|
|
chunkSize: label.length, |
884
|
|
|
dwName: dwName, |
885
|
|
|
value: label |
886
|
|
|
}); |
887
|
|
|
this.LIST[adtlIndex].chunkSize += label.length + 4 + 4 + 4 + 1; |
888
|
|
|
} |
889
|
|
|
|
890
|
|
|
/** |
891
|
|
|
* Return the index of the 'adtl' LIST in this.LIST. |
892
|
|
|
* @return {?number} |
893
|
|
|
* @private |
894
|
|
|
*/ |
895
|
|
|
getAdtlChunk_() { |
896
|
|
|
for (let i=0; i<this.LIST.length; i++) { |
897
|
|
|
if(this.LIST[i].format == 'adtl') { |
898
|
|
|
return i; |
899
|
|
|
} |
900
|
|
|
} |
901
|
|
|
return null; |
902
|
|
|
} |
903
|
|
|
|
904
|
|
|
/** |
905
|
|
|
* Return the index of a tag in a FILE chunk. |
906
|
|
|
* @param {string} tag The tag name. |
907
|
|
|
* @return {!Object<string, ?number>} |
908
|
|
|
* Object.LIST is the INFO index in LIST |
909
|
|
|
* Object.TAG is the tag index in the INFO |
910
|
|
|
* @private |
911
|
|
|
*/ |
912
|
|
|
getTagIndex_(tag) { |
913
|
|
|
/** @type {!Object<string, ?number>} */ |
914
|
|
|
let index = {LIST: null, TAG: null}; |
915
|
|
|
for (let i=0; i<this.LIST.length; i++) { |
916
|
|
|
if (this.LIST[i].format == 'INFO') { |
917
|
|
|
index.LIST = i; |
918
|
|
|
for (let j=0; j<this.LIST[i].subChunks.length; j++) { |
919
|
|
|
if (this.LIST[i].subChunks[j].chunkId == tag) { |
920
|
|
|
index.TAG = j; |
921
|
|
|
break; |
922
|
|
|
} |
923
|
|
|
} |
924
|
|
|
break; |
925
|
|
|
} |
926
|
|
|
} |
927
|
|
|
return index; |
928
|
|
|
} |
929
|
|
|
|
930
|
|
|
/** |
931
|
|
|
* Fix a RIFF tag format if possible, throw an error otherwise. |
932
|
|
|
* @param {string} tag The tag name. |
933
|
|
|
* @return {string} The tag name in proper fourCC format. |
934
|
|
|
* @private |
935
|
|
|
*/ |
936
|
|
|
fixTagName_(tag) { |
937
|
|
|
if (tag.constructor !== String) { |
938
|
|
|
throw new Error('Invalid tag name.'); |
939
|
|
|
} else if(tag.length < 4) { |
940
|
|
|
for (let i=0; i<4-tag.length; i++) { |
941
|
|
|
tag += ' '; |
942
|
|
|
} |
943
|
|
|
} |
944
|
|
|
return tag; |
945
|
|
|
} |
946
|
|
|
|
947
|
|
|
/** |
948
|
|
|
* Set up the WaveFile object from a byte buffer. |
949
|
|
|
* @param {!Uint8Array} buffer The buffer. |
950
|
|
|
* @param {boolean=} samples True if the samples should be loaded. |
951
|
|
|
* @throws {Error} If container is not RIFF, RIFX or RF64. |
952
|
|
|
* @throws {Error} If no 'fmt ' chunk is found. |
953
|
|
|
* @throws {Error} If no 'data' chunk is found. |
954
|
|
|
*/ |
955
|
|
|
readWavBuffer(buffer, samples=true) { |
956
|
|
|
this.io.head_ = 0; |
957
|
|
|
this.clearHeader_(); |
958
|
|
|
this.readRIFFChunk_(buffer); |
959
|
|
|
/** @type {!Object} */ |
960
|
|
|
let chunk = riffChunks(buffer); |
961
|
|
|
this.readDs64Chunk_(buffer, chunk.subChunks); |
962
|
|
|
this.readFmtChunk_(buffer, chunk.subChunks); |
963
|
|
|
this.readFactChunk_(buffer, chunk.subChunks); |
964
|
|
|
this.readBextChunk_(buffer, chunk.subChunks); |
965
|
|
|
this.readCueChunk_(buffer, chunk.subChunks); |
966
|
|
|
this.readSmplChunk_(buffer, chunk.subChunks); |
967
|
|
|
this.readDataChunk_(buffer, chunk.subChunks, samples); |
968
|
|
|
this.readJunkChunk_(buffer, chunk.subChunks); |
969
|
|
|
this.readLISTChunk_(buffer, chunk.subChunks); |
970
|
|
|
this.bitDepthFromFmt_(); |
971
|
|
|
this.updateDataType_(); |
972
|
|
|
} |
973
|
|
|
|
974
|
|
|
/** |
975
|
|
|
* Read the RIFF chunk a wave file. |
976
|
|
|
* @param {!Uint8Array} bytes A wav buffer. |
977
|
|
|
* @throws {Error} If no 'RIFF' chunk is found. |
978
|
|
|
* @private |
979
|
|
|
*/ |
980
|
|
|
readRIFFChunk_(bytes) { |
981
|
|
|
this.io.head_ = 0; |
982
|
|
|
this.container = this.io.readString_(bytes, 4); |
983
|
|
|
if (['RIFF', 'RIFX', 'RF64'].indexOf(this.container) === -1) { |
984
|
|
|
throw Error('Not a supported format.'); |
985
|
|
|
} |
986
|
|
|
this.LEorBE_(); |
987
|
|
|
this.chunkSize = this.io.read_(bytes, this.uInt32_); |
988
|
|
|
this.format = this.io.readString_(bytes, 4); |
989
|
|
|
if (this.format != 'WAVE') { |
990
|
|
|
throw Error('Could not find the "WAVE" format identifier'); |
991
|
|
|
} |
992
|
|
|
} |
993
|
|
|
|
994
|
|
|
/** |
995
|
|
|
* Read the 'fmt ' chunk of a wave file. |
996
|
|
|
* @param {!Uint8Array} buffer The wav file buffer. |
997
|
|
|
* @param {!Object} signature The file signature. |
998
|
|
|
* @throws {Error} If no 'fmt ' chunk is found. |
999
|
|
|
* @private |
1000
|
|
|
*/ |
1001
|
|
|
readFmtChunk_(buffer, signature) { |
1002
|
|
|
/** @type {?Object} */ |
1003
|
|
|
let chunk = findChunk_(signature, 'fmt '); |
1004
|
|
|
if (chunk) { |
1005
|
|
|
this.io.head_ = chunk.chunkData.start; |
1006
|
|
|
this.fmt.chunkId = chunk.chunkId; |
1007
|
|
|
this.fmt.chunkSize = chunk.chunkSize; |
1008
|
|
|
this.fmt.audioFormat = this.io.read_(buffer, this.uInt16_); |
1009
|
|
|
this.fmt.numChannels = this.io.read_(buffer, this.uInt16_); |
1010
|
|
|
this.fmt.sampleRate = this.io.read_(buffer, this.uInt32_); |
1011
|
|
|
this.fmt.byteRate = this.io.read_(buffer, this.uInt32_); |
1012
|
|
|
this.fmt.blockAlign = this.io.read_(buffer, this.uInt16_); |
1013
|
|
|
this.fmt.bitsPerSample = this.io.read_(buffer, this.uInt16_); |
1014
|
|
|
this.readFmtExtension_(buffer); |
1015
|
|
|
} else { |
1016
|
|
|
throw Error('Could not find the "fmt " chunk'); |
1017
|
|
|
} |
1018
|
|
|
} |
1019
|
|
|
|
1020
|
|
|
/** |
1021
|
|
|
* Read the 'fmt ' chunk extension. |
1022
|
|
|
* @param {!Uint8Array} buffer The wav file buffer. |
1023
|
|
|
* @private |
1024
|
|
|
*/ |
1025
|
|
|
readFmtExtension_(buffer) { |
1026
|
|
|
if (this.fmt.chunkSize > 16) { |
1027
|
|
|
this.fmt.cbSize = this.io.read_(buffer, this.uInt16_); |
1028
|
|
|
if (this.fmt.chunkSize > 18) { |
1029
|
|
|
this.fmt.validBitsPerSample = this.io.read_(buffer, this.uInt16_); |
1030
|
|
|
if (this.fmt.chunkSize > 20) { |
1031
|
|
|
this.fmt.dwChannelMask = this.io.read_(buffer, this.uInt32_); |
1032
|
|
|
this.fmt.subformat = [ |
1033
|
|
|
this.io.read_(buffer, this.uInt32_), |
1034
|
|
|
this.io.read_(buffer, this.uInt32_), |
1035
|
|
|
this.io.read_(buffer, this.uInt32_), |
1036
|
|
|
this.io.read_(buffer, this.uInt32_)]; |
1037
|
|
|
} |
1038
|
|
|
} |
1039
|
|
|
} |
1040
|
|
|
} |
1041
|
|
|
|
1042
|
|
|
/** |
1043
|
|
|
* Read the 'fact' chunk of a wav file. |
1044
|
|
|
* @param {!Uint8Array} buffer The wav file buffer. |
1045
|
|
|
* @param {!Object} signature The file signature. |
1046
|
|
|
* @private |
1047
|
|
|
*/ |
1048
|
|
|
readFactChunk_(buffer, signature) { |
1049
|
|
|
/** @type {?Object} */ |
1050
|
|
|
let chunk = findChunk_(signature, 'fact'); |
1051
|
|
|
if (chunk) { |
1052
|
|
|
this.io.head_ = chunk.chunkData.start; |
1053
|
|
|
this.fact.chunkId = chunk.chunkId; |
1054
|
|
|
this.fact.chunkSize = chunk.chunkSize; |
1055
|
|
|
this.fact.dwSampleLength = this.io.read_(buffer, this.uInt32_); |
1056
|
|
|
} |
1057
|
|
|
} |
1058
|
|
|
|
1059
|
|
|
/** |
1060
|
|
|
* Read the 'cue ' chunk of a wave file. |
1061
|
|
|
* @param {!Uint8Array} buffer The wav file buffer. |
1062
|
|
|
* @param {!Object} signature The file signature. |
1063
|
|
|
* @private |
1064
|
|
|
*/ |
1065
|
|
|
readCueChunk_(buffer, signature) { |
1066
|
|
|
/** @type {?Object} */ |
1067
|
|
|
let chunk = findChunk_(signature, 'cue '); |
1068
|
|
|
if (chunk) { |
1069
|
|
|
this.io.head_ = chunk.chunkData.start; |
1070
|
|
|
this.cue.chunkId = chunk.chunkId; |
1071
|
|
|
this.cue.chunkSize = chunk.chunkSize; |
1072
|
|
|
this.cue.dwCuePoints = this.io.read_(buffer, this.uInt32_); |
1073
|
|
|
for (let i=0; i<this.cue.dwCuePoints; i++) { |
1074
|
|
|
this.cue.points.push({ |
1075
|
|
|
dwName: this.io.read_(buffer, this.uInt32_), |
1076
|
|
|
dwPosition: this.io.read_(buffer, this.uInt32_), |
1077
|
|
|
fccChunk: this.io.readString_(buffer, 4), |
1078
|
|
|
dwChunkStart: this.io.read_(buffer, this.uInt32_), |
1079
|
|
|
dwBlockStart: this.io.read_(buffer, this.uInt32_), |
1080
|
|
|
dwSampleOffset: this.io.read_(buffer, this.uInt32_), |
1081
|
|
|
}); |
1082
|
|
|
} |
1083
|
|
|
} |
1084
|
|
|
} |
1085
|
|
|
|
1086
|
|
|
/** |
1087
|
|
|
* Read the 'smpl' chunk of a wave file. |
1088
|
|
|
* @param {!Uint8Array} buffer The wav file buffer. |
1089
|
|
|
* @param {!Object} signature The file signature. |
1090
|
|
|
* @private |
1091
|
|
|
*/ |
1092
|
|
|
readSmplChunk_(buffer, signature) { |
1093
|
|
|
/** @type {?Object} */ |
1094
|
|
|
let chunk = findChunk_(signature, 'smpl'); |
1095
|
|
|
if (chunk) { |
1096
|
|
|
this.io.head_ = chunk.chunkData.start; |
1097
|
|
|
this.smpl.chunkId = chunk.chunkId; |
1098
|
|
|
this.smpl.chunkSize = chunk.chunkSize; |
1099
|
|
|
this.smpl.dwManufacturer = this.io.read_(buffer, this.uInt32_); |
1100
|
|
|
this.smpl.dwProduct = this.io.read_(buffer, this.uInt32_); |
1101
|
|
|
this.smpl.dwSamplePeriod = this.io.read_(buffer, this.uInt32_); |
1102
|
|
|
this.smpl.dwMIDIUnityNote = this.io.read_(buffer, this.uInt32_); |
1103
|
|
|
this.smpl.dwMIDIPitchFraction = this.io.read_(buffer, this.uInt32_); |
1104
|
|
|
this.smpl.dwSMPTEFormat = this.io.read_(buffer, this.uInt32_); |
1105
|
|
|
this.smpl.dwSMPTEOffset = this.io.read_(buffer, this.uInt32_); |
1106
|
|
|
this.smpl.dwNumSampleLoops = this.io.read_(buffer, this.uInt32_); |
1107
|
|
|
this.smpl.dwSamplerData = this.io.read_(buffer, this.uInt32_); |
1108
|
|
|
for (let i=0; i<this.smpl.dwNumSampleLoops; i++) { |
1109
|
|
|
this.smpl.loops.push({ |
1110
|
|
|
dwName: this.io.read_(buffer, this.uInt32_), |
1111
|
|
|
dwType: this.io.read_(buffer, this.uInt32_), |
1112
|
|
|
dwStart: this.io.read_(buffer, this.uInt32_), |
1113
|
|
|
dwEnd: this.io.read_(buffer, this.uInt32_), |
1114
|
|
|
dwFraction: this.io.read_(buffer, this.uInt32_), |
1115
|
|
|
dwPlayCount: this.io.read_(buffer, this.uInt32_), |
1116
|
|
|
}); |
1117
|
|
|
} |
1118
|
|
|
} |
1119
|
|
|
} |
1120
|
|
|
|
1121
|
|
|
/** |
1122
|
|
|
* Read the 'data' chunk of a wave file. |
1123
|
|
|
* @param {!Uint8Array} buffer The wav file buffer. |
1124
|
|
|
* @param {!Object} signature The file signature. |
1125
|
|
|
* @param {boolean} samples True if the samples should be loaded. |
1126
|
|
|
* @throws {Error} If no 'data' chunk is found. |
1127
|
|
|
* @private |
1128
|
|
|
*/ |
1129
|
|
|
readDataChunk_(buffer, signature, samples) { |
1130
|
|
|
/** @type {?Object} */ |
1131
|
|
|
let chunk = findChunk_(signature, 'data'); |
1132
|
|
|
if (chunk) { |
1133
|
|
|
this.data.chunkId = 'data'; |
1134
|
|
|
this.data.chunkSize = chunk.chunkSize; |
1135
|
|
|
if (samples) { |
1136
|
|
|
this.data.samples = buffer.slice( |
1137
|
|
|
chunk.chunkData.start, |
1138
|
|
|
chunk.chunkData.end); |
1139
|
|
|
} |
1140
|
|
|
} else { |
1141
|
|
|
throw Error('Could not find the "data" chunk'); |
1142
|
|
|
} |
1143
|
|
|
} |
1144
|
|
|
|
1145
|
|
|
/** |
1146
|
|
|
* Read the 'bext' chunk of a wav file. |
1147
|
|
|
* @param {!Uint8Array} buffer The wav file buffer. |
1148
|
|
|
* @param {!Object} signature The file signature. |
1149
|
|
|
* @private |
1150
|
|
|
*/ |
1151
|
|
|
readBextChunk_(buffer, signature) { |
1152
|
|
|
/** @type {?Object} */ |
1153
|
|
|
let chunk = findChunk_(signature, 'bext'); |
1154
|
|
|
if (chunk) { |
1155
|
|
|
this.io.head_ = chunk.chunkData.start; |
1156
|
|
|
this.bext.chunkId = chunk.chunkId; |
1157
|
|
|
this.bext.chunkSize = chunk.chunkSize; |
1158
|
|
|
this.bext.description = this.io.readString_(buffer, 256); |
1159
|
|
|
this.bext.originator = this.io.readString_(buffer, 32); |
1160
|
|
|
this.bext.originatorReference = this.io.readString_(buffer, 32); |
1161
|
|
|
this.bext.originationDate = this.io.readString_(buffer, 10); |
1162
|
|
|
this.bext.originationTime = this.io.readString_(buffer, 8); |
1163
|
|
|
this.bext.timeReference = [ |
1164
|
|
|
this.io.read_(buffer, this.uInt32_), |
1165
|
|
|
this.io.read_(buffer, this.uInt32_)]; |
1166
|
|
|
this.bext.version = this.io.read_(buffer, this.uInt16_); |
1167
|
|
|
this.bext.UMID = this.io.readString_(buffer, 64); |
1168
|
|
|
this.bext.loudnessValue = this.io.read_(buffer, this.uInt16_); |
1169
|
|
|
this.bext.loudnessRange = this.io.read_(buffer, this.uInt16_); |
1170
|
|
|
this.bext.maxTruePeakLevel = this.io.read_(buffer, this.uInt16_); |
1171
|
|
|
this.bext.maxMomentaryLoudness = this.io.read_(buffer, this.uInt16_); |
1172
|
|
|
this.bext.maxShortTermLoudness = this.io.read_(buffer, this.uInt16_); |
1173
|
|
|
this.bext.reserved = this.io.readString_(buffer, 180); |
1174
|
|
|
this.bext.codingHistory = this.io.readString_( |
1175
|
|
|
buffer, this.bext.chunkSize - 602); |
1176
|
|
|
} |
1177
|
|
|
} |
1178
|
|
|
|
1179
|
|
|
/** |
1180
|
|
|
* Read the 'ds64' chunk of a wave file. |
1181
|
|
|
* @param {!Uint8Array} buffer The wav file buffer. |
1182
|
|
|
* @param {!Object} signature The file signature. |
1183
|
|
|
* @throws {Error} If no 'ds64' chunk is found and the file is RF64. |
1184
|
|
|
* @private |
1185
|
|
|
*/ |
1186
|
|
|
readDs64Chunk_(buffer, signature) { |
1187
|
|
|
/** @type {?Object} */ |
1188
|
|
|
let chunk = findChunk_(signature, 'ds64'); |
1189
|
|
|
if (chunk) { |
1190
|
|
|
this.io.head_ = chunk.chunkData.start; |
1191
|
|
|
this.ds64.chunkId = chunk.chunkId; |
1192
|
|
|
this.ds64.chunkSize = chunk.chunkSize; |
1193
|
|
|
this.ds64.riffSizeHigh = this.io.read_(buffer, this.uInt32_); |
1194
|
|
|
this.ds64.riffSizeLow = this.io.read_(buffer, this.uInt32_); |
1195
|
|
|
this.ds64.dataSizeHigh = this.io.read_(buffer, this.uInt32_); |
1196
|
|
|
this.ds64.dataSizeLow = this.io.read_(buffer, this.uInt32_); |
1197
|
|
|
this.ds64.originationTime = this.io.read_(buffer, this.uInt32_); |
1198
|
|
|
this.ds64.sampleCountHigh = this.io.read_(buffer, this.uInt32_); |
1199
|
|
|
this.ds64.sampleCountLow = this.io.read_(buffer, this.uInt32_); |
1200
|
|
|
//if (this.ds64.chunkSize > 28) { |
1201
|
|
|
// this.ds64.tableLength = unpack( |
1202
|
|
|
// chunkData.slice(28, 32), this.uInt32_); |
1203
|
|
|
// this.ds64.table = chunkData.slice( |
1204
|
|
|
// 32, 32 + this.ds64.tableLength); |
1205
|
|
|
//} |
1206
|
|
|
} else { |
1207
|
|
|
if (this.container == 'RF64') { |
1208
|
|
|
throw Error('Could not find the "ds64" chunk'); |
1209
|
|
|
} |
1210
|
|
|
} |
1211
|
|
|
} |
1212
|
|
|
|
1213
|
|
|
/** |
1214
|
|
|
* Read the 'LIST' chunks of a wave file. |
1215
|
|
|
* @param {!Uint8Array} buffer The wav file buffer. |
1216
|
|
|
* @param {!Object} signature The file signature. |
1217
|
|
|
* @private |
1218
|
|
|
*/ |
1219
|
|
|
readLISTChunk_(buffer, signature) { |
1220
|
|
|
/** @type {?Object} */ |
1221
|
|
|
let listChunks = findChunk_(signature, 'LIST', true); |
1222
|
|
|
if (listChunks === null) { |
1223
|
|
|
return; |
1224
|
|
|
} |
1225
|
|
|
for (let j=0; j < listChunks.length; j++) { |
1226
|
|
|
/** @type {!Object} */ |
1227
|
|
|
let subChunk = listChunks[j]; |
1228
|
|
|
this.LIST.push({ |
1229
|
|
|
chunkId: subChunk.chunkId, |
1230
|
|
|
chunkSize: subChunk.chunkSize, |
1231
|
|
|
format: subChunk.format, |
1232
|
|
|
subChunks: []}); |
1233
|
|
|
for (let x=0; x<subChunk.subChunks.length; x++) { |
1234
|
|
|
this.readLISTSubChunks_(subChunk.subChunks[x], |
1235
|
|
|
subChunk.format, buffer); |
1236
|
|
|
} |
1237
|
|
|
} |
1238
|
|
|
} |
1239
|
|
|
|
1240
|
|
|
/** |
1241
|
|
|
* Read the sub chunks of a 'LIST' chunk. |
1242
|
|
|
* @param {!Object} subChunk The 'LIST' subchunks. |
1243
|
|
|
* @param {string} format The 'LIST' format, 'adtl' or 'INFO'. |
1244
|
|
|
* @param {!Uint8Array} buffer The wav file buffer. |
1245
|
|
|
* @private |
1246
|
|
|
*/ |
1247
|
|
|
readLISTSubChunks_(subChunk, format, buffer) { |
1248
|
|
|
if (format == 'adtl') { |
1249
|
|
|
if (['labl', 'note','ltxt'].indexOf(subChunk.chunkId) > -1) { |
1250
|
|
|
this.io.head_ = subChunk.chunkData.start; |
1251
|
|
|
/** @type {!Object<string, string|number>} */ |
1252
|
|
|
let item = { |
1253
|
|
|
chunkId: subChunk.chunkId, |
1254
|
|
|
chunkSize: subChunk.chunkSize, |
1255
|
|
|
dwName: this.io.read_(buffer, this.uInt32_) |
1256
|
|
|
}; |
1257
|
|
|
if (subChunk.chunkId == 'ltxt') { |
1258
|
|
|
item.dwSampleLength = this.io.read_(buffer, this.uInt32_); |
1259
|
|
|
item.dwPurposeID = this.io.read_(buffer, this.uInt32_); |
1260
|
|
|
item.dwCountry = this.io.read_(buffer, this.uInt16_); |
1261
|
|
|
item.dwLanguage = this.io.read_(buffer, this.uInt16_); |
1262
|
|
|
item.dwDialect = this.io.read_(buffer, this.uInt16_); |
1263
|
|
|
item.dwCodePage = this.io.read_(buffer, this.uInt16_); |
1264
|
|
|
} |
1265
|
|
|
item.value = this.io.readZSTR_(buffer, this.io.head_); |
1266
|
|
|
this.LIST[this.LIST.length - 1].subChunks.push(item); |
1267
|
|
|
} |
1268
|
|
|
// RIFF INFO tags like ICRD, ISFT, ICMT |
1269
|
|
|
} else if(format == 'INFO') { |
1270
|
|
|
this.io.head_ = subChunk.chunkData.start; |
1271
|
|
|
this.LIST[this.LIST.length - 1].subChunks.push({ |
1272
|
|
|
chunkId: subChunk.chunkId, |
1273
|
|
|
chunkSize: subChunk.chunkSize, |
1274
|
|
|
value: this.io.readZSTR_(buffer, this.io.head_) |
1275
|
|
|
}); |
1276
|
|
|
} |
1277
|
|
|
} |
1278
|
|
|
|
1279
|
|
|
/** |
1280
|
|
|
* Read the 'junk' chunk of a wave file. |
1281
|
|
|
* @param {!Uint8Array} buffer The wav file buffer. |
1282
|
|
|
* @param {!Object} signature The file signature. |
1283
|
|
|
* @private |
1284
|
|
|
*/ |
1285
|
|
|
readJunkChunk_(buffer, signature) { |
1286
|
|
|
/** @type {?Object} */ |
1287
|
|
|
let chunk = findChunk_(signature, 'junk'); |
1288
|
|
|
if (chunk) { |
1289
|
|
|
this.junk = { |
1290
|
|
|
chunkId: chunk.chunkId, |
1291
|
|
|
chunkSize: chunk.chunkSize, |
1292
|
|
|
chunkData: [].slice.call(buffer.slice( |
1293
|
|
|
chunk.chunkData.start, |
1294
|
|
|
chunk.chunkData.end)) |
1295
|
|
|
}; |
1296
|
|
|
} |
1297
|
|
|
} |
1298
|
|
|
|
1299
|
|
|
/** |
1300
|
|
|
* Return the bytes of the 'bext' chunk. |
1301
|
|
|
* @return {!Array<number>} The 'bext' chunk bytes. |
1302
|
|
|
* @private |
1303
|
|
|
*/ |
1304
|
|
|
getBextBytes_() { |
1305
|
|
|
/** @type {!Array<number>} */ |
1306
|
|
|
let bytes = []; |
1307
|
|
|
this.enforceBext_(); |
1308
|
|
|
if (this.bext.chunkId) { |
1309
|
|
|
this.bext.chunkSize = 602 + this.bext.codingHistory.length; |
1310
|
|
|
bytes = bytes.concat( |
1311
|
|
|
packString(this.bext.chunkId), |
1312
|
|
|
pack(602 + this.bext.codingHistory.length, this.uInt32_), |
1313
|
|
|
this.io.writeString_(this.bext.description, 256), |
1314
|
|
|
this.io.writeString_(this.bext.originator, 32), |
1315
|
|
|
this.io.writeString_(this.bext.originatorReference, 32), |
1316
|
|
|
this.io.writeString_(this.bext.originationDate, 10), |
1317
|
|
|
this.io.writeString_(this.bext.originationTime, 8), |
1318
|
|
|
pack(this.bext.timeReference[0], this.uInt32_), |
1319
|
|
|
pack(this.bext.timeReference[1], this.uInt32_), |
1320
|
|
|
pack(this.bext.version, this.uInt16_), |
1321
|
|
|
this.io.writeString_(this.bext.UMID, 64), |
1322
|
|
|
pack(this.bext.loudnessValue, this.uInt16_), |
1323
|
|
|
pack(this.bext.loudnessRange, this.uInt16_), |
1324
|
|
|
pack(this.bext.maxTruePeakLevel, this.uInt16_), |
1325
|
|
|
pack(this.bext.maxMomentaryLoudness, this.uInt16_), |
1326
|
|
|
pack(this.bext.maxShortTermLoudness, this.uInt16_), |
1327
|
|
|
this.io.writeString_(this.bext.reserved, 180), |
1328
|
|
|
this.io.writeString_( |
1329
|
|
|
this.bext.codingHistory, this.bext.codingHistory.length)); |
1330
|
|
|
} |
1331
|
|
|
return bytes; |
1332
|
|
|
} |
1333
|
|
|
|
1334
|
|
|
/** |
1335
|
|
|
* Make sure a 'bext' chunk is created if BWF data was created in a file. |
1336
|
|
|
* @private |
1337
|
|
|
*/ |
1338
|
|
|
enforceBext_() { |
1339
|
|
|
for (var prop in this.bext) { |
1340
|
|
|
if (this.bext.hasOwnProperty(prop)) { |
1341
|
|
|
if (this.bext[prop] && prop != 'timeReference') { |
1342
|
|
|
this.bext.chunkId = 'bext'; |
1343
|
|
|
break; |
1344
|
|
|
} |
1345
|
|
|
} |
1346
|
|
|
} |
1347
|
|
|
if (this.bext.timeReference[0] || this.bext.timeReference[1]) { |
1348
|
|
|
this.bext.chunkId = 'bext'; |
1349
|
|
|
} |
1350
|
|
|
} |
1351
|
|
|
|
1352
|
|
|
/** |
1353
|
|
|
* Return the bytes of the 'ds64' chunk. |
1354
|
|
|
* @return {!Array<number>} The 'ds64' chunk bytes. |
1355
|
|
|
* @private |
1356
|
|
|
*/ |
1357
|
|
|
getDs64Bytes_() { |
1358
|
|
|
/** @type {!Array<number>} */ |
1359
|
|
|
let bytes = []; |
1360
|
|
|
if (this.ds64.chunkId) { |
1361
|
|
|
bytes = bytes.concat( |
1362
|
|
|
packString(this.ds64.chunkId), |
1363
|
|
|
pack(this.ds64.chunkSize, this.uInt32_), |
1364
|
|
|
pack(this.ds64.riffSizeHigh, this.uInt32_), |
1365
|
|
|
pack(this.ds64.riffSizeLow, this.uInt32_), |
1366
|
|
|
pack(this.ds64.dataSizeHigh, this.uInt32_), |
1367
|
|
|
pack(this.ds64.dataSizeLow, this.uInt32_), |
1368
|
|
|
pack(this.ds64.originationTime, this.uInt32_), |
1369
|
|
|
pack(this.ds64.sampleCountHigh, this.uInt32_), |
1370
|
|
|
pack(this.ds64.sampleCountLow, this.uInt32_)); |
1371
|
|
|
} |
1372
|
|
|
//if (this.ds64.tableLength) { |
1373
|
|
|
// ds64Bytes = ds64Bytes.concat( |
1374
|
|
|
// pack(this.ds64.tableLength, this.uInt32_), |
1375
|
|
|
// this.ds64.table); |
1376
|
|
|
//} |
1377
|
|
|
return bytes; |
1378
|
|
|
} |
1379
|
|
|
|
1380
|
|
|
/** |
1381
|
|
|
* Return the bytes of the 'cue ' chunk. |
1382
|
|
|
* @return {!Array<number>} The 'cue ' chunk bytes. |
1383
|
|
|
* @private |
1384
|
|
|
*/ |
1385
|
|
|
getCueBytes_() { |
1386
|
|
|
/** @type {!Array<number>} */ |
1387
|
|
|
let bytes = []; |
1388
|
|
|
if (this.cue.chunkId) { |
1389
|
|
|
/** @type {!Array<number>} */ |
1390
|
|
|
let cuePointsBytes = this.getCuePointsBytes_(); |
1391
|
|
|
bytes = bytes.concat( |
1392
|
|
|
packString(this.cue.chunkId), |
1393
|
|
|
pack(cuePointsBytes.length + 4, this.uInt32_), |
1394
|
|
|
pack(this.cue.dwCuePoints, this.uInt32_), |
1395
|
|
|
cuePointsBytes); |
1396
|
|
|
} |
1397
|
|
|
return bytes; |
1398
|
|
|
} |
1399
|
|
|
|
1400
|
|
|
/** |
1401
|
|
|
* Return the bytes of the 'cue ' points. |
1402
|
|
|
* @return {!Array<number>} The 'cue ' points as an array of bytes. |
1403
|
|
|
* @private |
1404
|
|
|
*/ |
1405
|
|
|
getCuePointsBytes_() { |
1406
|
|
|
/** @type {!Array<number>} */ |
1407
|
|
|
let points = []; |
1408
|
|
|
for (let i=0; i<this.cue.dwCuePoints; i++) { |
1409
|
|
|
points = points.concat( |
1410
|
|
|
pack(this.cue.points[i].dwName, this.uInt32_), |
1411
|
|
|
pack(this.cue.points[i].dwPosition, this.uInt32_), |
1412
|
|
|
packString(this.cue.points[i].fccChunk), |
1413
|
|
|
pack(this.cue.points[i].dwChunkStart, this.uInt32_), |
1414
|
|
|
pack(this.cue.points[i].dwBlockStart, this.uInt32_), |
1415
|
|
|
pack(this.cue.points[i].dwSampleOffset, this.uInt32_)); |
1416
|
|
|
} |
1417
|
|
|
return points; |
1418
|
|
|
} |
1419
|
|
|
|
1420
|
|
|
/** |
1421
|
|
|
* Return the bytes of the 'smpl' chunk. |
1422
|
|
|
* @return {!Array<number>} The 'smpl' chunk bytes. |
1423
|
|
|
* @private |
1424
|
|
|
*/ |
1425
|
|
|
getSmplBytes_() { |
1426
|
|
|
/** @type {!Array<number>} */ |
1427
|
|
|
let bytes = []; |
1428
|
|
|
if (this.smpl.chunkId) { |
1429
|
|
|
/** @type {!Array<number>} */ |
1430
|
|
|
let smplLoopsBytes = this.getSmplLoopsBytes_(); |
1431
|
|
|
bytes = bytes.concat( |
1432
|
|
|
packString(this.smpl.chunkId), |
1433
|
|
|
pack(smplLoopsBytes.length + 36, this.uInt32_), |
1434
|
|
|
pack(this.smpl.dwManufacturer, this.uInt32_), |
1435
|
|
|
pack(this.smpl.dwProduct, this.uInt32_), |
1436
|
|
|
pack(this.smpl.dwSamplePeriod, this.uInt32_), |
1437
|
|
|
pack(this.smpl.dwMIDIUnityNote, this.uInt32_), |
1438
|
|
|
pack(this.smpl.dwMIDIPitchFraction, this.uInt32_), |
1439
|
|
|
pack(this.smpl.dwSMPTEFormat, this.uInt32_), |
1440
|
|
|
pack(this.smpl.dwSMPTEOffset, this.uInt32_), |
1441
|
|
|
pack(this.smpl.dwNumSampleLoops, this.uInt32_), |
1442
|
|
|
pack(this.smpl.dwSamplerData, this.uInt32_), |
1443
|
|
|
smplLoopsBytes); |
1444
|
|
|
} |
1445
|
|
|
return bytes; |
1446
|
|
|
} |
1447
|
|
|
|
1448
|
|
|
/** |
1449
|
|
|
* Return the bytes of the 'smpl' loops. |
1450
|
|
|
* @return {!Array<number>} The 'smpl' loops as an array of bytes. |
1451
|
|
|
* @private |
1452
|
|
|
*/ |
1453
|
|
|
getSmplLoopsBytes_() { |
1454
|
|
|
/** @type {!Array<number>} */ |
1455
|
|
|
let loops = []; |
1456
|
|
|
for (let i=0; i<this.smpl.dwNumSampleLoops; i++) { |
1457
|
|
|
loops = loops.concat( |
1458
|
|
|
pack(this.smpl.loops[i].dwName, this.uInt32_), |
1459
|
|
|
pack(this.smpl.loops[i].dwType, this.uInt32_), |
1460
|
|
|
pack(this.smpl.loops[i].dwStart, this.uInt32_), |
1461
|
|
|
pack(this.smpl.loops[i].dwEnd, this.uInt32_), |
1462
|
|
|
pack(this.smpl.loops[i].dwFraction, this.uInt32_), |
1463
|
|
|
pack(this.smpl.loops[i].dwPlayCount, this.uInt32_)); |
1464
|
|
|
} |
1465
|
|
|
return loops; |
1466
|
|
|
} |
1467
|
|
|
|
1468
|
|
|
/** |
1469
|
|
|
* Return the bytes of the 'fact' chunk. |
1470
|
|
|
* @return {!Array<number>} The 'fact' chunk bytes. |
1471
|
|
|
* @private |
1472
|
|
|
*/ |
1473
|
|
|
getFactBytes_() { |
1474
|
|
|
/** @type {!Array<number>} */ |
1475
|
|
|
let bytes = []; |
1476
|
|
|
if (this.fact.chunkId) { |
1477
|
|
|
bytes = bytes.concat( |
1478
|
|
|
packString(this.fact.chunkId), |
1479
|
|
|
pack(this.fact.chunkSize, this.uInt32_), |
1480
|
|
|
pack(this.fact.dwSampleLength, this.uInt32_)); |
1481
|
|
|
} |
1482
|
|
|
return bytes; |
1483
|
|
|
} |
1484
|
|
|
|
1485
|
|
|
/** |
1486
|
|
|
* Return the bytes of the 'fmt ' chunk. |
1487
|
|
|
* @return {!Array<number>} The 'fmt' chunk bytes. |
1488
|
|
|
* @throws {Error} if no 'fmt ' chunk is present. |
1489
|
|
|
* @private |
1490
|
|
|
*/ |
1491
|
|
|
getFmtBytes_() { |
1492
|
|
|
/** @type {!Array<number>} */ |
1493
|
|
|
let fmtBytes = []; |
1494
|
|
|
if (this.fmt.chunkId) { |
1495
|
|
|
return fmtBytes.concat( |
1496
|
|
|
packString(this.fmt.chunkId), |
1497
|
|
|
pack(this.fmt.chunkSize, this.uInt32_), |
1498
|
|
|
pack(this.fmt.audioFormat, this.uInt16_), |
1499
|
|
|
pack(this.fmt.numChannels, this.uInt16_), |
1500
|
|
|
pack(this.fmt.sampleRate, this.uInt32_), |
1501
|
|
|
pack(this.fmt.byteRate, this.uInt32_), |
1502
|
|
|
pack(this.fmt.blockAlign, this.uInt16_), |
1503
|
|
|
pack(this.fmt.bitsPerSample, this.uInt16_), |
1504
|
|
|
this.getFmtExtensionBytes_()); |
1505
|
|
|
} |
1506
|
|
|
throw Error('Could not find the "fmt " chunk'); |
1507
|
|
|
} |
1508
|
|
|
|
1509
|
|
|
/** |
1510
|
|
|
* Return the bytes of the fmt extension fields. |
1511
|
|
|
* @return {!Array<number>} The fmt extension bytes. |
1512
|
|
|
* @private |
1513
|
|
|
*/ |
1514
|
|
|
getFmtExtensionBytes_() { |
1515
|
|
|
/** @type {!Array<number>} */ |
1516
|
|
|
let extension = []; |
1517
|
|
|
if (this.fmt.chunkSize > 16) { |
1518
|
|
|
extension = extension.concat( |
1519
|
|
|
pack(this.fmt.cbSize, this.uInt16_)); |
1520
|
|
|
} |
1521
|
|
|
if (this.fmt.chunkSize > 18) { |
1522
|
|
|
extension = extension.concat( |
1523
|
|
|
pack(this.fmt.validBitsPerSample, this.uInt16_)); |
1524
|
|
|
} |
1525
|
|
|
if (this.fmt.chunkSize > 20) { |
1526
|
|
|
extension = extension.concat( |
1527
|
|
|
pack(this.fmt.dwChannelMask, this.uInt32_)); |
1528
|
|
|
} |
1529
|
|
|
if (this.fmt.chunkSize > 24) { |
1530
|
|
|
extension = extension.concat( |
1531
|
|
|
pack(this.fmt.subformat[0], this.uInt32_), |
1532
|
|
|
pack(this.fmt.subformat[1], this.uInt32_), |
1533
|
|
|
pack(this.fmt.subformat[2], this.uInt32_), |
1534
|
|
|
pack(this.fmt.subformat[3], this.uInt32_)); |
1535
|
|
|
} |
1536
|
|
|
return extension; |
1537
|
|
|
} |
1538
|
|
|
|
1539
|
|
|
/** |
1540
|
|
|
* Return the bytes of the 'LIST' chunk. |
1541
|
|
|
* @return {!Array<number>} The 'LIST' chunk bytes. |
1542
|
|
|
*/ |
1543
|
|
|
getLISTBytes_() { |
1544
|
|
|
/** @type {!Array<number>} */ |
1545
|
|
|
let bytes = []; |
1546
|
|
|
for (let i=0; i<this.LIST.length; i++) { |
1547
|
|
|
/** @type {!Array<number>} */ |
1548
|
|
|
let subChunksBytes = this.getLISTSubChunksBytes_( |
1549
|
|
|
this.LIST[i].subChunks, this.LIST[i].format); |
1550
|
|
|
bytes = bytes.concat( |
1551
|
|
|
packString(this.LIST[i].chunkId), |
1552
|
|
|
pack(subChunksBytes.length + 4, this.uInt32_), |
1553
|
|
|
packString(this.LIST[i].format), |
1554
|
|
|
subChunksBytes); |
1555
|
|
|
} |
1556
|
|
|
return bytes; |
1557
|
|
|
} |
1558
|
|
|
|
1559
|
|
|
/** |
1560
|
|
|
* Return the bytes of the sub chunks of a 'LIST' chunk. |
1561
|
|
|
* @param {!Array<!Object>} subChunks The 'LIST' sub chunks. |
1562
|
|
|
* @param {string} format The format of the 'LIST' chunk. |
1563
|
|
|
* Currently supported values are 'adtl' or 'INFO'. |
1564
|
|
|
* @return {!Array<number>} The sub chunk bytes. |
1565
|
|
|
* @private |
1566
|
|
|
*/ |
1567
|
|
|
getLISTSubChunksBytes_(subChunks, format) { |
1568
|
|
|
/** @type {!Array<number>} */ |
1569
|
|
|
let bytes = []; |
1570
|
|
|
for (let i=0; i<subChunks.length; i++) { |
1571
|
|
|
if (format == 'INFO') { |
1572
|
|
|
bytes = bytes.concat( |
1573
|
|
|
packString(subChunks[i].chunkId), |
1574
|
|
|
pack(subChunks[i].value.length + 1, this.uInt32_), |
1575
|
|
|
this.io.writeString_( |
1576
|
|
|
subChunks[i].value, subChunks[i].value.length)); |
1577
|
|
|
bytes.push(0); |
1578
|
|
|
} else if (format == 'adtl') { |
1579
|
|
|
if (['labl', 'note'].indexOf(subChunks[i].chunkId) > -1) { |
1580
|
|
|
bytes = bytes.concat( |
1581
|
|
|
packString(subChunks[i].chunkId), |
1582
|
|
|
pack( |
1583
|
|
|
subChunks[i].value.length + 4 + 1, this.uInt32_), |
1584
|
|
|
pack(subChunks[i].dwName, this.uInt32_), |
1585
|
|
|
this.io.writeString_( |
1586
|
|
|
subChunks[i].value, |
1587
|
|
|
subChunks[i].value.length)); |
1588
|
|
|
bytes.push(0); |
1589
|
|
|
} else if (subChunks[i].chunkId == 'ltxt') { |
1590
|
|
|
bytes = bytes.concat( |
1591
|
|
|
this.getLtxtChunkBytes_(subChunks[i])); |
1592
|
|
|
} |
1593
|
|
|
} |
1594
|
|
|
if (bytes.length % 2) { |
1595
|
|
|
bytes.push(0); |
1596
|
|
|
} |
1597
|
|
|
} |
1598
|
|
|
return bytes; |
1599
|
|
|
} |
1600
|
|
|
|
1601
|
|
|
/** |
1602
|
|
|
* Return the bytes of a 'ltxt' chunk. |
1603
|
|
|
* @param {!Object} ltxt the 'ltxt' chunk. |
1604
|
|
|
* @return {!Array<number>} The 'ltxt' chunk bytes. |
1605
|
|
|
* @private |
1606
|
|
|
*/ |
1607
|
|
|
getLtxtChunkBytes_(ltxt) { |
1608
|
|
|
return [].concat( |
1609
|
|
|
packString(ltxt.chunkId), |
1610
|
|
|
pack(ltxt.value.length + 20, this.uInt32_), |
1611
|
|
|
pack(ltxt.dwName, this.uInt32_), |
1612
|
|
|
pack(ltxt.dwSampleLength, this.uInt32_), |
1613
|
|
|
pack(ltxt.dwPurposeID, this.uInt32_), |
1614
|
|
|
pack(ltxt.dwCountry, this.uInt16_), |
1615
|
|
|
pack(ltxt.dwLanguage, this.uInt16_), |
1616
|
|
|
pack(ltxt.dwDialect, this.uInt16_), |
1617
|
|
|
pack(ltxt.dwCodePage, this.uInt16_), |
1618
|
|
|
this.io.writeString_(ltxt.value, ltxt.value.length)); |
1619
|
|
|
} |
1620
|
|
|
|
1621
|
|
|
/** |
1622
|
|
|
* Return the bytes of the 'junk' chunk. |
1623
|
|
|
* @return {!Array<number>} The 'junk' chunk bytes. |
1624
|
|
|
* @private |
1625
|
|
|
*/ |
1626
|
|
|
getJunkBytes_() { |
1627
|
|
|
/** @type {!Array<number>} */ |
1628
|
|
|
let bytes = []; |
1629
|
|
|
if (this.junk.chunkId) { |
1630
|
|
|
return bytes.concat( |
1631
|
|
|
packString(this.junk.chunkId), |
1632
|
|
|
pack(this.junk.chunkData.length, this.uInt32_), |
1633
|
|
|
this.junk.chunkData); |
1634
|
|
|
} |
1635
|
|
|
return bytes; |
1636
|
|
|
} |
1637
|
|
|
|
1638
|
|
|
/** |
1639
|
|
|
* Return a .wav file byte buffer with the data from the WaveFile object. |
1640
|
|
|
* The return value of this method can be written straight to disk. |
1641
|
|
|
* @return {!Uint8Array} The wav file bytes. |
1642
|
|
|
* @private |
1643
|
|
|
*/ |
1644
|
|
|
createWaveFile_() { |
1645
|
|
|
/** @type {!Array<!Array<number>>} */ |
1646
|
|
|
let fileBody = [ |
1647
|
|
|
this.getJunkBytes_(), |
1648
|
|
|
this.getDs64Bytes_(), |
1649
|
|
|
this.getBextBytes_(), |
1650
|
|
|
this.getFmtBytes_(), |
1651
|
|
|
this.getFactBytes_(), |
1652
|
|
|
packString(this.data.chunkId), |
1653
|
|
|
pack(this.data.samples.length, this.uInt32_), |
1654
|
|
|
this.data.samples, |
1655
|
|
|
this.getCueBytes_(), |
1656
|
|
|
this.getSmplBytes_(), |
1657
|
|
|
this.getLISTBytes_() |
1658
|
|
|
]; |
1659
|
|
|
/** @type {number} */ |
1660
|
|
|
let fileBodyLength = 0; |
1661
|
|
|
for (let i=0; i<fileBody.length; i++) { |
1662
|
|
|
fileBodyLength += fileBody[i].length; |
1663
|
|
|
} |
1664
|
|
|
/** @type {!Uint8Array} */ |
1665
|
|
|
let file = new Uint8Array(fileBodyLength + 12); |
1666
|
|
|
/** @type {number} */ |
1667
|
|
|
let index = 0; |
1668
|
|
|
index = packStringTo(this.container, file, index); |
1669
|
|
|
index = packTo(fileBodyLength + 4, this.uInt32_, file, index); |
1670
|
|
|
index = packStringTo(this.format, file, index); |
1671
|
|
|
for (let i=0; i<fileBody.length; i++) { |
1672
|
|
|
file.set(fileBody[i], index); |
1673
|
|
|
index += fileBody[i].length; |
1674
|
|
|
} |
1675
|
|
|
return file; |
1676
|
|
|
} |
1677
|
|
|
|
1678
|
|
|
/** |
1679
|
|
|
* Reset attributes that should emptied when a file is |
1680
|
|
|
* created with the fromScratch() or fromBuffer() methods. |
1681
|
|
|
* @private |
1682
|
|
|
*/ |
1683
|
|
|
clearHeader_() { |
1684
|
|
|
this.fmt.cbSize = 0; |
1685
|
|
|
this.fmt.validBitsPerSample = 0; |
1686
|
|
|
this.fact.chunkId = ''; |
1687
|
|
|
this.ds64.chunkId = ''; |
1688
|
|
|
} |
1689
|
|
|
|
1690
|
|
|
/** |
1691
|
|
|
* Set up to work wih big-endian or little-endian files. |
1692
|
|
|
* The types used are changed to LE or BE. If the |
1693
|
|
|
* the file is big-endian (RIFX), true is returned. |
1694
|
|
|
* @return {boolean} True if the file is RIFX. |
1695
|
|
|
* @private |
1696
|
|
|
*/ |
1697
|
|
|
LEorBE_() { |
1698
|
|
|
/** @type {boolean} */ |
1699
|
|
|
let bigEndian = this.container === 'RIFX'; |
1700
|
|
|
this.uInt16_.be = bigEndian; |
1701
|
|
|
this.uInt32_.be = bigEndian; |
1702
|
|
|
return bigEndian; |
1703
|
|
|
} |
1704
|
|
|
|
1705
|
|
|
/** |
1706
|
|
|
* Update the type definition used to read and write the samples. |
1707
|
|
|
* @private |
1708
|
|
|
*/ |
1709
|
|
|
updateDataType_() { |
1710
|
|
|
/** @type {!Object} */ |
1711
|
|
|
this.dataType = { |
1712
|
|
|
bits: ((parseInt(this.bitDepth, 10) - 1) | 7) + 1, |
1713
|
|
|
float: this.bitDepth == '32f' || this.bitDepth == '64', |
1714
|
|
|
signed: this.bitDepth != '8', |
1715
|
|
|
be: this.container == 'RIFX' |
1716
|
|
|
}; |
1717
|
|
|
if (['4', '8a', '8m'].indexOf(this.bitDepth) > -1 ) { |
1718
|
|
|
this.dataType.bits = 8; |
1719
|
|
|
this.dataType.signed = false; |
1720
|
|
|
} |
1721
|
|
|
} |
1722
|
|
|
|
1723
|
|
|
/** |
1724
|
|
|
* Set the string code of the bit depth based on the 'fmt ' chunk. |
1725
|
|
|
* @private |
1726
|
|
|
*/ |
1727
|
|
|
bitDepthFromFmt_() { |
1728
|
|
|
if (this.fmt.audioFormat === 3 && this.fmt.bitsPerSample === 32) { |
1729
|
|
|
this.bitDepth = '32f'; |
1730
|
|
|
} else if (this.fmt.audioFormat === 6) { |
1731
|
|
|
this.bitDepth = '8a'; |
1732
|
|
|
} else if (this.fmt.audioFormat === 7) { |
1733
|
|
|
this.bitDepth = '8m'; |
1734
|
|
|
} else { |
1735
|
|
|
this.bitDepth = this.fmt.bitsPerSample.toString(); |
1736
|
|
|
} |
1737
|
|
|
} |
1738
|
|
|
|
1739
|
|
|
/** |
1740
|
|
|
* Return 'RIFF' if the container is 'RF64', the current container name |
1741
|
|
|
* otherwise. Used to enforce 'RIFF' when RF64 is not allowed. |
1742
|
|
|
* @return {string} |
1743
|
|
|
* @private |
1744
|
|
|
*/ |
1745
|
|
|
correctContainer_() { |
1746
|
|
|
return this.container == 'RF64' ? 'RIFF' : this.container; |
1747
|
|
|
} |
1748
|
|
|
} |
1749
|
|
|
|